/******************************************************************************
 * Arquivo: VeiculoDAOImpl.java                                                           *
 * Autor: Aurindo Jorge Ferreira                                              *
 * e-mail: aurindo@gmail.com                                                  *
 * Data de cria��o: 09/12/2007                                                   *
 *                                                                            *
 * Propriedade de FastFind Solu��es Tecnologicas                              *
 ******************************************************************************/

package br.com.srv.componentes.veiculo.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import HTTPClient.HTTPConnection;
import br.com.srv.dao.WebServiceBaseDAO;
import br.com.srv.model.ComandoTO;
import br.com.srv.model.PontoTO;
import br.com.srv.model.VeiculoTO;
import br.com.srv.util.ArenaHelper;
import br.com.srv.util.DateHelper;

public class VeiculoDAOImpl extends WebServiceBaseDAO 
	implements VeiculoDAO {
	
	public VeiculoDAOImpl(HTTPConnection connection) {
		setHttpConnection(connection);
	}

	public boolean bloquearVeiculoById(VeiculoTO veiculoTO) throws Exception {
		
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER);
		urlString.append("<COMMAND_DATA_TERMINAL>");
		urlString.append("<SERIAL>1</SERIAL>");
		urlString.append("<TEXT>" + ComandoTO.COMANDO_BLOQUEAR + "[</TEXT>");
		urlString.append("<ATTEMPTS>1</ATTEMPTS>");
		urlString.append("<VEHICLES>");
		urlString.append("<VEHICLE_ID>" + veiculoTO.getId() + "</VEHICLE_ID>");
		urlString.append("</VEHICLES>");
		urlString.append("</COMMAND_DATA_TERMINAL>");
		
		System.out.println(urlString);
		String xml = queryArenaMethod(
				ArenaHelper.WEB_SERVICE_MTCDATATERMINALFREETEXT, urlString);
						
		return isResultaoOk(xml);
	}

	private boolean isResultaoOk(String xml) throws Exception {
		if (!xml.equals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><RESULT>OK</RESULT>")) {
			System.out.println("[ERRO:] " + xml);
			return false;
		}
		
		return true;
	}

	public boolean desbloquearVeiculoById(VeiculoTO veiculoTO) throws Exception {
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER);
		urlString.append("<COMMAND_DATA_TERMINAL>");
		urlString.append("<SERIAL>1</SERIAL>");
		urlString.append("<TEXT>" + ComandoTO.COMANDO_DESBLOQUEAR + "[</TEXT>");
		urlString.append("<ATTEMPTS>1</ATTEMPTS>");
		urlString.append("<VEHICLES>");
		urlString.append("<VEHICLE_ID>" + veiculoTO.getId() + "</VEHICLE_ID>");
		urlString.append("</VEHICLES>");
		urlString.append("</COMMAND_DATA_TERMINAL>");
		
		System.out.println(urlString);
		String xml = queryArenaMethod(
				ArenaHelper.WEB_SERVICE_MTCDATATERMINALFREETEXT, urlString);
						
		return isResultaoOk(xml);
	}

	public List<PontoTO> buscarPosicoes(Integer veiculoId,
			String dataInicial, String horaInicial,
			String dataFinal, String horaFinal) throws Exception {
		
		dataInicial = DateHelper.format("yyyy-MM-dd", dataInicial);
		dataFinal = DateHelper.format("yyyy-MM-dd", dataFinal);

		StringBuilder urlString = new StringBuilder(ArenaHelper.XML_READER);
		urlString.append("<COURSES>");
		urlString.append("	<VEHICLES>");
		urlString.append("		<VEHICLE_ID>" + veiculoId.toString() + "</VEHICLE_ID>");
		urlString.append("	</VEHICLES>");
		urlString.append("	<START>" + dataInicial + "</START>");
		urlString.append("	<END>" + dataFinal + "</END>");
		urlString.append("	<PERIOD_PER_DAY>1</PERIOD_PER_DAY>");
		urlString.append("	<PERIOD_START>" + horaInicial + "</PERIOD_START>");
		urlString.append("	<PERIOD_END>" + horaFinal + "</PERIOD_END>");
		urlString.append("	<VEL_ANALYSIS>0</VEL_ANALYSIS>");
		urlString.append("	<VEL>0</VEL>");
		urlString.append("	<DECREASING>0</DECREASING>");
		urlString.append("	<LIMIT>");
		urlString.append("		<COUNT>0</COUNT>");
		urlString.append("		<QUANT>0</QUANT>");
		urlString.append("		<OFFSET>0</OFFSET>");
		urlString.append("	</LIMIT>");
		urlString.append("	<SHOW_FIELDS>");
		urlString.append("		<ID>1</ID>");
		urlString.append("		<DESC>1</DESC>");
		urlString.append("		<NEAR_DESC>1</NEAR_DESC>");
		urlString.append("		<DIST_BETWEEN_POSITIONS>1</DIST_BETWEEN_POSITIONS>");
		urlString.append("		<DATE_TIME>21</DATE_TIME>");
		urlString.append("		<Y>1</Y>");
		urlString.append("		<X>1</X>");
		urlString.append("		<VEL>1</VEL>");
		urlString.append("		<NEAR_DIST>1</NEAR_DIST>");
		urlString.append("	</SHOW_FIELDS>");
		urlString.append("</COURSES>");

		String xml = queryArenaMethod(ArenaHelper.WEB_SERVICE_MAPCOURSES, urlString);

		List<Map> result = parseList(xml, "/COURSES/VEHICLE", "POSITION");
		
		List<PontoTO> posicoes = new ArrayList<PontoTO>();
		for (Map mapa : result) {
			PontoTO pontoTO = new PontoTO();
			pontoTO.setDataHora((String)mapa.get("DATE_TIME"));
			pontoTO.setLongitude(new Double((String)mapa.get("X")));
			pontoTO.setLatitude(new Double((String)mapa.get("Y")));	
			pontoTO.setVelocidade(new Double((String)mapa.get("VEL")));	
			pontoTO.setDistanciaUltimoPonto(new Double((String)mapa.get("DIST_BETWEEN_POSITIONS")));
			pontoTO.setDescricao((String)mapa.get("NEAR_DESC"));
			posicoes.add(pontoTO);
		}
		
		return posicoes;
	}

	//não implementar
	public List<Map<String, String>> buscarUltimasPosicaoVeiculos(
			List<String> idVeiculos) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String buscarSerialbyId(String veiculoId) {
		// TODO Auto-generated method stub
		return null;
	}
}
